package com.eastfair.auth.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.eastfair.auth.dto.OrganizationDTO;
import com.eastfair.auth.entity.Organization;
import com.eastfair.auth.enumeration.OrganizationOrgTypeEnum;
import com.eastfair.auth.service.OrganizationService;
import com.eastfair.auth.service.ResourceOrganizationService;
import com.eastfair.auth.service.SponsorUserOrganizationService;
import com.eastfair.auth.service.UserOrganizationService;
import com.eastfair.core.base.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会展主办操作业务实现类
 *
 * </p>
 *
 * @author ligang
 * @date 2021-07-16
 */
@Slf4j
@Service
@DS("#thread.tenant")
public class SponsorUserOrganizationServiceImpl implements SponsorUserOrganizationService {

    @Autowired
    OrganizationService organizationService;

    @Autowired
    ResourceOrganizationService resourceOrganizationService;

    @Autowired
    UserOrganizationService userOrganizationService;

    @Override
    public R<Organization> createSponsorClassifyOfProject(Long orgId, Long projectId) {
        OrganizationDTO organizationDTO = new OrganizationDTO();
        organizationDTO.setName("项目下的主办分类");
        organizationDTO.setOrgType(OrganizationOrgTypeEnum.P_SPONSOR_CLASSIFY);
        organizationDTO.setParentId(orgId);
        organizationDTO.setProjectId(projectId);
        return organizationService.createOrg(organizationDTO);
    }

    @Override
    public R<Organization> createSponsorClassify(Long orgId, Long projectId) {
        OrganizationDTO organizationDTO = new OrganizationDTO();
        organizationDTO.setName("主办组");
        organizationDTO.setOrgType(OrganizationOrgTypeEnum.SPONSOR_CLASSIFY);
        organizationDTO.setParentId(orgId);
        organizationDTO.setProjectId(projectId);
        return organizationService.createOrg(organizationDTO);
    }

    @Override
    public R<Organization> addSponsorOfProjectClassify(Long projectId, Long sponsorId) {
        //查询项目下的主办分类的节点
        Organization organization = organizationService.queryOrgOfType(projectId, OrganizationOrgTypeEnum.P_SPONSOR_CLASSIFY);
        if (organization != null) {
            OrganizationDTO organizationDTO = new OrganizationDTO();
            //主办分类的父节点id
            organizationDTO.setParentId(organization.getId());
            organizationDTO.setOrgType(OrganizationOrgTypeEnum.OTHER);
            organizationDTO.setProjectId(projectId);
            organizationDTO.setResourceId(sponsorId);
            //添加主办到项目下的主办分类
            return organizationService.createOrg(organizationDTO);
        }
        return R.fail("操作失败");
    }

    @Override
    public R<Organization> addHostByProject(OrganizationDTO organizationDTO) {
        //查询主办组节点
        Organization organizationClassify = organizationService.queryOrgOfType(organizationDTO.getProjectId(), OrganizationOrgTypeEnum.SPONSOR_CLASSIFY);
        if (organizationClassify != null) {
            //创建项目下的公司信息
            organizationDTO.setParentId(organizationClassify.getId());
            organizationDTO.setOrgType(OrganizationOrgTypeEnum.SPONSOR);
            R<Organization> r = organizationService.createOrg(organizationDTO);
            Organization organization = r.getData();
            //加入到项目下的主办分类里
            addSponsorOfProjectClassify(organization.getProjectId(), organizationDTO.getResourceId());
            //创建主办员工
            OrganizationDTO userOrganizationDTO = new OrganizationDTO();
            userOrganizationDTO.setOrgType(OrganizationOrgTypeEnum.STAFF);
            userOrganizationDTO.setName("员工");
            userOrganizationDTO.setParentId(organization.getId());
            userOrganizationDTO.setProjectId(organizationDTO.getProjectId());
            organizationService.createOrg(userOrganizationDTO);
            return R.success(organization);
        }
        return R.fail("操作失败");
    }

}
